#!/usr/local/bin/perl
# Copyright (c) 2000-2006 ActiveState Software Inc.
# See the file LICENSE.txt for licensing information.

Import(
    'cons',
    'build',
    'idlExportDir',
    'mozComponentsDir',
    'ranRegxpcomStateFileName',
    'mozBin',
    'mozIncludePath',
    'mozIdlIncludePath',
    'mozLibPath',
    'mozSrc',
    'mozDevelDist',
    'mozObjDir',
    'platform',
    'architecture',
    'buildType',
    'supportDir',
    'withSymbols',
    'unsiloedPerlExe',
    'unsiloedPythonExe',
    'mozVersionNumber',
    'mozVersion',
    'mozDist',
    'sdkDir',
);


# koIContentUtils
$cons->Install($idlExportDir, 'koIContentUtils.idl');
$cons->BuildAndInstallXpt('koIContentUtils.idl');
$cons->BuildHeaderFromIdl('koIContentUtils.idl');

%consLocal = $cons->copy();

if ($platform eq "win") {
    my $CXXFLAGS = $consLocal{'CXXFLAGS'};
    my $LDFLAGS = $consLocal{'LDFLAGS'};
    foreach my $includeDir (split(';', $mozIncludePath)) {
        $CXXFLAGS .= " -I$includeDir";
    }
    $CXXFLAGS .= " -I$mozDevelDist/include/content" .
        " -I$mozDevelDist/include/js" .
        " -I$mozDevelDist/include/pref" .
        " -I$mozDevelDist/include/docshell" .
        " -I$mozDevelDist/include/xpconnect" .
        " -I$mozDevelDist/include/necko" .
        " -I$mozDevelDist/include/layout";

    # override the construction environment as necessary:
    #   - specify a DLL
    #   - link in some Mozilla libs
    #   - CXXFLAGS is basically a copy of the that obtained via Mozilla's
    #     build makefiles, huge, mostly probably not necessary
    foreach my $libDir (split(';', $mozLibPath)) {
        $LDFLAGS .= " /libpath:$libDir";
    }

    $CXXFLAGS .= ' -I./include';
    $CXXFLAGS .= ' -I./include/private';
    $LDFLAGS .= " xul.lib xpcomglue_s.lib nspr4.lib mozjs.lib mozalloc.lib ";
    $LDFLAGS .= " /dll";
    $CXXFLAGS .= " -DLAYERS -W3 -D_WINDOWS -DWIN32 -DMOZILLA_INTERNAL_API=1 ";
    $CXXFLAGS .= " -D_IMPL_NS_LAYOUT -DMOZ_VERSION=$mozVersionNumber";
    $CXXFLAGS .= " /FI$mozObjDir\\mozilla-config.h";

    if ($buildType eq 'debug') {
        $LDFLAGS .= " /DEBUG";
        $CXXFLAGS .= " /Od -Zi  -DDEBUG  -D_DEBUG -MDd";
    } else {
        $CXXFLAGS .= " /Ox -DNDEBUG -D_NDEBUG -MD";
        # LDFLAGS is fine!
    }
    $dllName = 'koContentUtils.dll';

    $cons->Command($dllName,
        "koContentUtils.cpp",
        "koIContentUtils.idl",
        "koIContentUtils.h",
        qq(
            cl /nologo $CXXFLAGS /Fo%1:b.obj /c %1
            link /nologo $LDFLAGS /out:%0 %1:b.obj
        )
    );
}

else {
    %consLocal = $cons->copy();

    $consLocal{'LIBPATH'} = $mozLibPath;
    $consLocal{'CPPPATH'} = "$mozIncludePath";
    $consLocal{'CXXFLAGS'} = $consLocal{ENV}{'CXXFLAGS'} . " -I$mozDevelDist/include/content" .
        " -I$mozDevelDist/include/js" .
        " -I$mozDevelDist/include/pref" .
        " -I$mozDevelDist/include/docshell" .
        " -I$mozDevelDist/include/xpconnect" .
        " -I$mozDevelDist/include/necko" .
        " -I$mozDevelDist/include/layout";
    $consLocal{'LDFLAGS'} = $consLocal{ENV}{'LDFLAGS'};

    if ($platform eq "linux" && $buildType ne 'debug') {
	# Debug builds will fail with missing "gTLSThreadID" if this is enabled.
	$consLocal{'LDFLAGS'} .= " -Wl,-z,defs";
    }


    # Force linking of libxul first, otherwise it will complain about
    # missing MOZ_Z_xxx symbols - as libjs_static only has empty symbols for
    # them.
    if ($platform eq "darwin") {
        $consLocal{'LIBS'} = "$mozDevelDist/lib/XUL ";
        $consLocal{'LDFLAGS'} .= " -framework Carbon -bundle";
    } else {
        $consLocal{'LIBS'} = "-lxul ";
        $consLocal{'LDFLAGS'} .= " -fPIC -shared";
    }
    $consLocal{'LIBS'} .= "-lmozalloc -lxpcomglue_s -lnspr4 ";
    $consLocal{'CXXFLAGS'} .= " -DMOZILLA_INTERNAL_API=1 -D_IMPL_NS_LAYOUT";
    $consLocal{'CXXFLAGS'} .= " -include $mozObjDir/mozilla-config.h -DMOZ_VERSION=$mozVersionNumber";

    if ($platform eq "solaris") {

	# big-endian byte order indicator to license code
        $consLocal{'CXXFLAGS'} .= " -DBYTEORDER=0x4321";
    }
    # On Solaris, you have to use the -mimpure-text option if you are linking
    # a shared library that links to static libs.
    if ($platform eq "solaris") {
        $consLocal{'LDFLAGS'} .= " -mimpure-text";
    }

    if ($buildType eq 'debug') {
        $consLocal{'CXXFLAGS'} .= " -g -DDEBUG -D_DEBUG";
    } else {
        $consLocal{'CXXFLAGS'} .= " -DNDEBUG -D_NDEBUG";
        if ($withSymbols) {
            $consLocal{'CXXFLAGS'} .= " -g";
        }
    }
    if ($platform eq "darwin") {
      $consLocal{'DYLD_LIBRARY_PATH'} = "$mozSrc/mozilla/dist/bin";
      $dllName = "libkoContentUtils.dylib";
    } else {
      $dllName = 'libkoContentUtils.so';
    }
    $consLocal = new cons(%consLocal);

    $consLocal->Program($dllName,
                        "koContentUtils.cpp");
}

$cons->InstallXpcomComponent($dllName);

